package com.xiangzi.spark;

import static spark.Spark.*;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import spark.ModelAndView;
import spark.template.freemarker.FreeMarkerEngine;

public class Application {

	private static Logger logger = LoggerFactory.getLogger(Application.class);

	public static void main(String[] args) {
		// "trace", "debug", "info", "warn", or "error"
		// System.setProperty("org.slf4j.simpleLogger.defaultLogLevel",
		// "trace");
		// Logger logger = LoggerFactory.getLogger(Application.class);

		port(9090); // Spark will run on port 9090

		// staticFiles.location("/public"); // Static files

		boolean localhost = true;
		// How do I enable automatic refresh of static files?
		if (localhost) {
			String projectDir = System.getProperty("user.dir");
			String staticDir = "/src/main/resources/public";

			logger.debug("projectDir:" + projectDir);
			logger.debug("staticDir:" + staticDir);

			staticFiles.externalLocation(projectDir + staticDir);
		} else {
			staticFiles.location("/public");
		}

		get("/hello", (req, res) -> "Hello World");

		get("/", (request, response) -> {
			// Show something

			return "";
		});

		post("/", (request, response) -> {
			// Create something

			return "";
		});

		put("/", (request, response) -> {
			// Update something

			return "";
		});

		delete("/", (request, response) -> {
			// Annihilate something

			return "";
		});

		options("/", (request, response) -> {
			// Appease something

			return "";
		});

		// matches "GET /hello/foo" and "GET /hello/bar"
		// request.params(":name") is 'foo' or 'bar'
		get("/hello/:name", (request, response) -> {
			return "Hello: " + request.params(":name");
		});

		// matches "GET /say/hello/to/world"
		// request.splat()[0] is 'hello' and request.splat()[1] 'world'
		get("/say/*/to/*", (request, response) -> {
			return "Number of splat parameters: " + request.splat().length;
		});

		/**
		 * Filters
		 */
		before((request, response) -> {
			boolean authenticated;
			// ... check if authenticated

			authenticated = true;

			if (!authenticated) {
				halt(401, "You are not welcome here");
			}
		});

		after((request, response) -> {
			response.header("foo", "set by after filter");
		});

		before("/protected/*", (request, response) -> {
			// ... check if authenticated
			halt(401, "Go Away!");
		});

		/**
		 * Exception Mapping <br/>
		 * To handle exceptions of a configured type for all routes and filters:
		 */
		// get("/throwexception", (request, response) -> {
		// throw new YourCustomException();
		// });
		//
		// exception(YourCustomException.class, (exception, request, response)
		// -> {
		// // Handle the exception here
		// });

		/**
		 * Views and Templates
		 */
		get("/hello/view/freemarker", (request, response) -> {
			Map<String, Object> attributes = new HashMap<>();
			attributes.put("message", "Hello World!");

			// The hello.ftl file is located in directory:
			// src/test/resources/spark/template/freemarker
			return new ModelAndView(attributes, "hello.ftl");
		}, new FreeMarkerEngine());

	}

}
